import { Player } from "@minecraft/server";
import { Game, GameComponent } from "@sapi-game/main";
import { DimensionIds, EntityTypeIds } from "@sapi-game/utils/vanila-data";
import { isReferee } from "@/func";
import { MonopolyBackState } from "@/monopoly/states/backState";
import { MonopolyGame } from "@/monopoly/game";

export class MonopolyGameStarter extends GameComponent<MonopolyBackState> {
    override onAttach(): void {
        this.subscribe(
            Game.events.buttonPush,
            (e) => {
                const p = e.source as Player;
                if (!isReferee(p)) {
                    return p.sendMessage("你不是裁判，无法开始游戏");
                }
                if (Game.manager.hasGame(MonopolyGame)) {
                    return p.sendMessage(
                        "游戏尚未结束，如需结束，请执行/game stop " +
                            MonopolyGame.name
                    );
                }
                Game.manager.startGame(MonopolyGame);
            },
            {
                dimensionId: DimensionIds.Overworld,
                loc: [-194, -35, 254],
                sourceType: EntityTypeIds.Player,
            }
        );
    }
}
